<?php
define("APPID", "wx5672506691fea394"); //appid值
define("APPSECRET", "a10b06bf4f7519152bad03311f212e65"); //密钥

//define("APPID", "wx6d03a0a1108166e1"); //appid值
//define("APPSECRET", "1d25a94f42f8167185b47a14d3bf4c18"); //密钥
/**
 * Created by PhpStorm.
 * User: steven
 * Date: 2016/4/20
 * Time: 22:59
 */
class AccessTokenClass
{
    protected $CI;

    // We'll use a constructor, as you can't directly call a function
    // from a property definition.
    public function __construct()
    {
        // Assign the CodeIgniter super-object
        $this->CI =& get_instance();
        $this->CI->load->model('Access_Token_Model');
    }

//    public function get_access_token(){
//
//
//
//        return "";
//    }

    public function get_access_token(){
        $access_token_array=$this->CI->Access_Token_Model->get_sys_access_token();
        if(empty($access_token_array)||count($access_token_array)==0){
            $accessToken=$this->get_new_access_token();
            $data=array(
                'gmt_create'=>date("Y-m-d H:i:s" ,time()),
                'gmt_modify'=>date("Y-m-d H:i:s" ,time()),
                'token_type'=>'1',
                'expire_time'=>date("Y-m-d H:i:s" ,time()+7200),
                'access_tokens'=>$accessToken
            );
            $result=$this->CI->Access_Token_Model->insert_access_token($data);
            if($result){
                return $accessToken;
            }else{
                return "";
            }
        }else{
            $access_token_row=$access_token_array[0];
            $expire_time=$access_token_row['expire_time'];
            //echo  'expire_time is:'.$expire_time;
            if((strtotime($expire_time)-200)<time()){
                return $this->expire_access_token();
            }else{
                return $access_token_row['access_tokens'];
            }
        }
    }

    public function get_new_access_token(){
        $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . APPSECRET;
        $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
//echo $res;
        $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
        $access_token = $result['access_token'];
        return $access_token;
    }
    public function  expire_access_token(){
        $access_token=$this->get_new_access_token();
    //    print_r('the access_token is '.$access_token);
        $access_token_array=$this->CI->Access_Token_Model->get_sys_access_token();
        $access_token_row=$access_token_array[0];
        if(!isset($access_token_row)){
            return false;
        }
        $data=array(
            'id'=>$access_token_row['id'],
            'gmt_modify'=>date("Y-m-d H:i:s" ,time()),
            'expire_time'=>date("Y-m-d H:i:s" ,time()+7200),
            'access_tokens'=>$access_token
        );

        $result=$this->CI->Access_Token_Model->update_access_token($data);
        if($result){
            return  $access_token;
        }else{
            return false;
        }
    }

//    public function auth_user_info(){
//        $this->CI->
//    }
}